#include "LevelSector.hpp"
#include "LightShip.hpp"
#include "StructureEntity.hpp"
#include "../SceneManager.hpp"

using namespace std;
/*
LevelSector::LevelSector(SECTOR_HEADER &secHeader, char** pDataBuffer, string textureName) : Entity3D(*pDataBuffer, secHeader.m_iSizeSectorMeshData, textureName)
{
	//! Move pointer by size of block of sector mesh data.
	*pDataBuffer += secHeader.m_iSizeSectorMeshData;

	for(int i = 0; i < secHeader.m_iNumUnits; ++i)
	{
		createUnit(pDataBuffer);
	}

	for(int i = 0; i < secHeader.m_iNumStructures; ++i)
	{
		createStructure(pDataBuffer);
	}
}*/

LevelSector::LevelSector(string sectorName, string textureName) : Entity3D(sectorName, textureName)
{
	SceneManager::GetInstance()->RegisterEntity(this);
}

LevelSector::~LevelSector()
{
}

bool LevelSector::createStructure(string structName, string textureName, float* pPos)
{
	bool   bSuccess = true;
	MVector3D position;

	position.x = pPos[0];
	position.y = pPos[1];
	position.z = pPos[2];

	if(strcmp(structName.c_str(), "building_1.gmf") == 0)
	{
		StructureEntity* pBuilding = new StructureEntity(structName, textureName, position);
		SceneManager::GetInstance()->RegisterEntity(pBuilding);
		const int id = pBuilding->getEntityID();
		m_sectorObjectsID.push_back(id);
	}

	return bSuccess;
}

bool LevelSector::createUnit(string unitName, string textureName, float *pPos, float& fSpeed)
{
	bool   bSuccess = true;
	MVector3D position;

	position.x = pPos[0];
	position.y = pPos[1];
	position.z = pPos[2];

	if(strcmp(unitName.c_str(), "lightShip_1.gmf") == 0)
	{
		LightShip* pShip = pShip = new LightShip(unitName, textureName, fSpeed, position);
		SceneManager::GetInstance()->RegisterEntity(pShip);
		const int id = pShip->getEntityID();
		m_sectorObjectsID.push_back(id);
	}
	else if(strcmp(unitName.c_str(), "hosHeavyShip") == 0)
	{
	}
	else if(strcmp(unitName.c_str(), "hosAsteroid") == 0)
	{
	}

	return bSuccess;
}

void LevelSector::changeSectorVisibility(bool bVisibility)
{
	std::vector<int>::iterator currElement = m_sectorObjectsID.begin();
	std::vector<int>::iterator endElement  = m_sectorObjectsID.end();

	SceneManager* pScnMgr = SceneManager::GetInstance();

	setVisibility(bVisibility);

	for(currElement; currElement != endElement; ++currElement)
	{
		BaseEntity* pEntity = pScnMgr->GetEntityByID((*currElement));
		pEntity->setVisibility(bVisibility);
	}
}

//EOF
